package com.app.view.zoon;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.util.AttributeSet;
import android.view.View;

import com.app.view.R;
import com.app.view.ScreenUtils;


/**
 * Created by xkh on 2017/10/31.
 * 绑定页面，画勾或者叉的动画view
 */
public class HookOrForkView extends View {
    // 绘制圆弧的进度值
    private int progress = 0;
    // 线1的x轴
    private int line1_x = 0;
    // 线1的y轴
    private int line1_y = 0;
    // 线2的x轴
    private int line2_x = 0;
    // 线2的y轴
    private int line2_y = 0;

    private int line_offsetx;
    private int line_offsety;
    private int offset;
    private int disturbFreshcount = 0;
    private boolean isStart = false;
    private boolean isDrawtrue = true;
    private float strokeW = 30;
    private int scaleW = 50;

    public HookOrForkView(Context context) {
        super(context);
    }

    public HookOrForkView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HookOrForkView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    // 绘制

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        strokeW = ScreenUtils.initScreen((Activity) getContext()).dp2px(4);//8
        scaleW = ScreenUtils.initScreen((Activity) getContext()).dp2px(10);//20

        if (!isStart)
            return;
        /**
         * 绘制圆弧
         */
        Paint paint = new Paint();
        // 设置中心园画笔颜色
        paint.setColor(getResources().getColor(R.color.color_fecb00));
        // 设置圆弧的宽度
        paint.setStrokeWidth(strokeW);
        // 设置圆弧为空心
        paint.setStyle(Paint.Style.FILL);
        // 消除锯齿
        paint.setAntiAlias(true);
        // 获取圆心的x坐标
        int center = getWidth() / 2;
//        int center1 = center - getWidth() / 5;
        int center1 = center * 2 / 4;// 确保钩可以画在居中位置
        // 圆弧半径
        int radius = getWidth() / 2;
        //连接成功，画内圆弧
        if (isDrawtrue) {
            radius = getWidth() / 2 - scaleW;
            progress += 6;
            if (progress <= scaleW) {
                canvas.drawCircle(center, getHeight() / 2, radius + progress,
                        paint);
            } else {
                canvas.drawCircle(center, getHeight() / 2, radius + scaleW, paint);
            }

            // 设置连接成功，对勾画笔颜色
            paint.setColor(getResources().getColor(R.color.color_white));
            paint.setStrokeCap(Cap.ROUND);

            /**
             *先等圆弧画完，才画对勾
             */
            if (progress >= scaleW) {

                if (line1_x < center / 3) {
                    line1_x += 10;
                    line1_y += 10;
                }
                if (line1_x >= center / 3) {
                    int offset = line1_x - center / 3;
                    line1_x = center / 3;
                    line1_y = line1_y - offset;
                }
                // 画第一根线
                canvas.drawLine(center1, center, center1 + line1_x, center
                        + line1_y, paint);

                if (line1_x >= center / 3) {

                    if (line2_x < (radius + scaleW) * 2 / 3) {
                        line2_x += 10;

                    }
                    if (line2_y < (radius + scaleW) * 2 / 3) {
                        line2_y += 10;
                    }


                    // 画第二根线
                    // canvas.drawLine(center1 + line1_x, center + line1_y,
                    // center
                    // + line2_x, center - line2_x, paint);
                    canvas.drawLine(center1 + line1_x, center + line1_y,
                            center1 + line1_x + line2_x, center + line1_y
                                    - line2_y, paint);


//                  勾画成功后，需断开刷新
                    if (line2_x >= (radius + scaleW) * 2 / 3 && line2_y >= (radius + scaleW) * 2 / 3) {
                        disturbFreshcount++;
//                        DebugLog.d("----------------line2_x + disturbFreshcount:" + disturbFreshcount);
                    }

                }

            }
            // 每隔10毫秒界面刷新
            if (disturbFreshcount < 2) {
                postInvalidateDelayed(1);
            }

        } else {

            canvas.drawCircle(center, getHeight() / 2, radius + progress, paint);
            // 设置画笔颜色
            //      paint.setColor(0xFF4166f5);
            paint.setColor(getResources().getColor(R.color.color_white));

            paint.setStrokeCap(Cap.ROUND);
            if (line_offsetx < radius / 2) {
                line_offsetx += 3;
                line_offsety = getHeight() / 2;

                if (line_offsetx > radius * 2 / 3)
                    line_offsetx = radius * 2 / 3;

                canvas.drawLine(center - line_offsetx, line_offsety, center
                        + line_offsetx, line_offsety, paint);
            } else {
                if (offset < radius / 2) {
                    offset += 3;
                    if (offset > radius / 4)
                        offset = radius / 4;

                }
                canvas.drawLine(center - line_offsetx + offset, line_offsety
                        - offset, center + line_offsetx - offset, line_offsety
                        + offset, paint);

                canvas.drawLine(center - line_offsetx + offset, line_offsety
                        + offset, center + line_offsetx - offset, line_offsety
                        - offset, paint);


                // 叉画成功后，需断开刷新
                if (offset == radius / 4) {
                    disturbFreshcount++;
//                    DebugLog.d("----------------offset + disturbFreshcount:" + disturbFreshcount);
                }

            }

            if (disturbFreshcount < 2) {
                postInvalidateDelayed(1);
            }
        }

    }

    public void startAnima() {
        this.isStart = true;
        reSet();
        invalidate();
    }

    public void stopAnima() {
        this.isStart = false;
        invalidate();
    }

    public void reSet() {
        progress = 0;
        line1_x = 0;
        line1_y = 0;
        line2_x = 0;
        line2_y = 0;
        line_offsetx = 0;
        line_offsety = 0;
        offset = 0;
        disturbFreshcount = 0;
    }

    // 设置画勾还是画叉
    public void setIsdrawtrue(boolean isTrue) {
        this.isDrawtrue = isTrue;
    }

}
